<?php

declare(strict_types=1);

namespace app\api\controller\v1;


use think\facade\Request;
use app\api\service\StatusCode;
use app\api\service\UserToken;
use app\api\model\Week as ModelWeek;
use app\api\model\Chapter as ModelChapter;
use app\api\validate\ParamMustBePostiveInt;
use app\api\validate\WeekCourse as ValidateWeekCourse;

/**
 * 教师端课程管理类
 */
class WeekCourse extends Api
{

    /**
     * 
     * 新增周期内容
     */

    public function create()
    {
        $data = Request::post();
        validate(ValidateWeekCourse::class)->scene('createWeekCourse')->check($data);
        $data["user_id"] = UserToken::getCurrentUid();
        $data = ModelWeek::addWeekCourse($data);
        if (empty($data->toArray())) {
            exit(json_encode([
                'code' => 400,
                'message' => '新增失败'
            ]));
        }
        $this->return_msg(StatusCode::SUCCESS, '新增成功', $data);
    }

    /**
     * 
     * 修改状态
     */
    public function edit()
    {
        $id = (new ParamMustBePostiveInt())->paramCheck();
        $data = Request::get();
        validate(ValidateWeekCourse::class)->scene('editWeekCourse')->check($data);
        $success = ModelWeek::editWeekCourse($id, $data['status']);
        if ($success == false) {
            exit(json_encode([
                'code' => 404,
                'message' => '修改失败'
            ]));
        }
        $this->return_msg(StatusCode::SUCCESS, '修改成功', $success);
    }

    /**
     * 
     * 删除周期课程
     */
    public function delete()
    {
        $id = (new ParamMustBePostiveInt())->paramCheck();
        $week = ModelWeek::find($id);
        if (empty($week)) {
            exit(json_encode([
                    'code' => 404,
                    'message' => '数据信息错误'
                ]));
        }
        $week_ = ModelWeek::getWeek($week->stage_id, 'desc', 'find');
        $maxWeek_name = $week_->week_name;
        if ($maxWeek_name != $week->week_name) {
            exit(json_encode([
                'code' => 404,
                'message' => '删除失败,仅能删除最后一周'
            ]));
        }
        $chapter = ModelChapter::where('week_id', $id)->find();
        if (!empty($chapter)) {
            exit(json_encode([
                'code'    => 404,
                'message' => '删除失败,请先删除章节'
            ]));
        }
        if ($week->delete() === true) {
            exit(json_encode([
                'code'    =>  200,
                'message' => '删除成功',
            ]));
        } else {
            exit(json_encode([
                'code'    =>  400,
                'message' => '删除失败',
            ]));
        };
    }

    /**
     * 
     * 编辑周期课程
     */

    public function update()
    {
        $weekId = (new ParamMustBePostiveInt())->paramCheck();
        $data = Request::post();
        validate(ValidateWeekCourse::class)->scene('updateWeekCourse')->check($data);
        $success = ModelWeek::updateWeekCourse($weekId, $data);
        $this->return_msg(StatusCode::SUCCESS, '修改成功', $success);
    }
}
